var table = null;
function initColumn () {
    return [
        {field: 'selectItem', radio: true},
        {title: '手机', field: 'phone', align: 'center', valign: 'middle',width:'50px'},
        {title: '姓名', field: 'username', align: 'center', valign: 'middle'},
        {title: '管理员类型', field: 'adminType', align: 'center', valign: 'middle',formatter:function (value,row,index) {
            if(value == 1){return "平台管理员";}
            if(value == 2){return "平台成员";}
            if(value == 3){return "商户管理员";}
            if(value == 4){return "商户成员";}
        }},
        {title: '管理员状态', field: 'adminStatus', align: 'center', valign: 'middle',formatter:function (value,row,index) {
            if(value == 0){return "正常"}
            if(value == 1) {return "禁用"}
        }},
        {title: '创建日期', field: 'createDate', align: 'center', valign: 'middle'},
        {title: '修改日期', field: 'updateDate', align: 'center', valign: 'middle'}
    ];
};
$(function () {
    var defaultColumns = initColumn();
    var searchParams = {};
    table = new BSTable('adminTable', "/platform/admin/list", defaultColumns,searchParams);
    table.setPaginationType("server");
    table.init();
    vm.allRoles();
});

var vm = new Vue({
    el: '#app',
    data: {
        showList: true,
        title:null,
        admin:{},
        role:{},
        roles:[],
        isUpdate:false,
    },
    methods:{
        add:function () {
            vm.admin = {};
            vm.role = {};
            vm.showList = false;
            vm.isUpdate = false;
        },
        update:function () {
            var data = $.getSelectData("adminTable");
            if (!data)
                return;
            vm.showList = false;
            vm.admin = data;
            vm.isUpdate = true;
        },
        del:function () {
            var data = $.getSelectData("adminTable");
            if (!data)
                return;
            window.confirm("确定要删除这条数据？",function () {
                $.ajax({
                    url:'/platform/admin/'+data.adminId,
                    dataType:'JSON',
                    success:function (result) {
                        if (result.code == 200){
                            alert("操作成功",function () {
                                table.refresh();
                            });
                        }else
                            alert(result.msg);
                    },
                    error:function (e) {
                        console.log("request error",e);
                        alert("请求失败");
                    }
                })
            });
        },
        saveOrUpdate:function () {
            var urlStr = vm.isUpdate ? "/platform/admin/update":"/platform/admin/add";
            $.ajax({
                url:urlStr,
                type:'POST',
                dataType:'JSON',
                data:JSON.stringify(vm.admin),
                contentType:"application/json;charset=UTF-8",
                success:function (result) {
                    if (result.code == 200){
                        alert("操作成功",function () {
                            vm.showList = true;
                            table.refresh();
                        });
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.error("request error",e);
                    alert("请求失败");
                }
            })
        },
        cancel:function () {
            vm.showList = true;
        },
        allRoles:function () {
            $.ajax({
                url:'/platform/role/list?pageNumber=1&pageSize=1000',
                dataType:'JSON',
                success:function (result) {
                        vm.roles = result.rows;
                },
                error:function (e) {
                    console.error("request error",e);
                    alert("请求失败");
                }
            })
        }
    }
});

